#include"sys.h"
uint32 BurstLength = 0;
extern MsgList* InitControlMMsg();


uint32 CreateSI()
{
	uint32 usr=0;
	while(tableMU2MI.usrCount<=MAXUSER)
	{
		while(tableMU2MI.usrAddr[usr]!=NULL)
		{
			usr++;
		}
		tableMU2MI.usrAddr[usr]=(SI *)calloc(sizeof(SI),1);
		tableMU2MI.usrCount++;
		return usr;
	}
	printf("CID is full \n");
	return 0;
}

void InitMsgTLVTable()
{
	Init_DCD_TLV_TABLE();
	Init_UCD_TLV_TABLE();
	InitRngRspTLVTable();
	InitRngReqTLVTable();

}
void InitSysUIUCTable()
{
	//QPSK rate 1/2
	sys.tableUIUC[0].modulation=2;
	sys.tableUIUC[0].rateUp    =1;
	sys.tableUIUC[0].rateDown  =2;
	//QPSK 3/4
	sys.tableUIUC[1].modulation=2;
	sys.tableUIUC[1].rateUp    =3;
	sys.tableUIUC[1].rateDown  =4;
	//16QAM rate1/2
	sys.tableUIUC[2].modulation=4;
	sys.tableUIUC[2].rateUp    =1;
	sys.tableUIUC[2].rateDown  =2;
	//16QAM rate 3/4
	sys.tableUIUC[3].modulation=4;
	sys.tableUIUC[3].rateUp    =3;
	sys.tableUIUC[3].rateDown  =4;
	//64QAM rate 1/2
	sys.tableUIUC[4].modulation=6;
	sys.tableUIUC[4].rateUp    =1;
	sys.tableUIUC[4].rateDown  =2;
	//64QAM rate 2/3
	sys.tableUIUC[5].modulation=6;
	sys.tableUIUC[5].rateUp    =2;
	sys.tableUIUC[6].rateDown  =3;
}
void InitSysDIUCTable()
{
	//QPSK rate 1/2
	sys.tableDIUC[0].modulation=2;
	sys.tableDIUC[0].rateUp    =1;
	sys.tableDIUC[0].rateDown  =2;
	//QPSK 3/4
	sys.tableDIUC[1].modulation=2;
	sys.tableDIUC[1].rateUp    =3;
	sys.tableDIUC[1].rateDown  =4;
	//16QAM rate1/2
	sys.tableDIUC[2].modulation=4;
	sys.tableDIUC[2].rateUp    =1;
	sys.tableDIUC[2].rateDown  =2;
	//16QAM rate 3/4
	sys.tableDIUC[3].modulation=4;
	sys.tableDIUC[3].rateUp    =3;
	sys.tableDIUC[3].rateDown  =4;
	//64QAM rate 1/2
	sys.tableDIUC[4].modulation=6;
	sys.tableDIUC[4].rateUp    =1;
	sys.tableDIUC[4].rateDown  =2;
	//64QAM rate 2/3
	sys.tableDIUC[5].modulation=6;
	sys.tableDIUC[5].rateUp    =2;
	sys.tableDIUC[6].rateDown  =3;
}
void InitSysInfo()
{
	memset(&sys,0,sizeof(SYSI));
	InitSysUIUCTable();
	InitSysDIUCTable();
	sys.sysULSlots      = UMXSLOTS;
	sys.sysDLSlots      = DMXSLOTS;
	sys.dlMsgQEmptCount = 0;
	sys.dlSrvQEmptCount = 4;
	sys.ulSrvQEmptCount = 0;
	sys.dlSlotPos       = 0;
	sys.rmdPDUCMIT      = NULL;
}

void InitMU2MIulQ(uint32 usr , uint32 index,uint32 Qempty,uint32 type ,uint32 prio)
{
	tableMU2MI.usrAddr[usr]->ulQueues.qosInfoPrio[index]=(QIP *)malloc(sizeof(QIP ));
	tableMU2MI.usrAddr[usr]->ulQueues.isQempty[index]=Qempty;
	tableMU2MI.usrAddr[usr]->ulQueues.qosInfoPrio[index]->priority=prio;
	tableMU2MI.usrAddr[usr]->ulQueues.qosInfoPrio[index]->service_type=type;
}
void InitMU2MIdlQ(uint32 usr , uint32 index,uint32 Qempty,uint32 type ,uint32 prio)
{
	tableMU2MI.usrAddr[usr]->dlQueues.qosInfoPrio[index]=(QIP *)malloc(sizeof(QIP ));
	tableMU2MI.usrAddr[usr]->dlQueues.isQempty[index]=Qempty;
	tableMU2MI.usrAddr[usr]->dlQueues.qosInfoPrio[index]->priority=prio;
	tableMU2MI.usrAddr[usr]->dlQueues.qosInfoPrio[index]->service_type=type;
}
void InitMU2MI(uint32 iusr)
{
//	uint32 iusr=0;
//	for(iusr=0;iusr<tableMU2MI.usrCount;iusr++)
//	{
		//tableMU2MI.usrAddr[i]=0;
		//tableMU2MI.usrAddr[i]->ulQueues=(ULQS *)malloc(sizeof(ULQS));
		//usr service_index isQempty type priority
		InitMU2MIulQ(iusr,0,1,0,1);
		InitMU2MIulQ(iusr,1,1,1,1);
		InitMU2MIulQ(iusr,2,1,2,1);
		InitMU2MIulQ(iusr,3,1,3,1);
		InitMU2MIulQ(iusr,4,1,4,1);
		InitMU2MIdlQ(iusr,0,1,0,1);
		InitMU2MIdlQ(iusr,1,1,1,1);
		InitMU2MIdlQ(iusr,2,1,2,1);
		InitMU2MIdlQ(iusr,3,1,3,1);

		tableMU2MI.usrAddr[iusr]->DIUC=1;
		tableMU2MI.usrAddr[iusr]->UIUC=1;
//	}
}

void InitCMT(CMT *table)
{
	(table)->Bsc      = NULL;
	(table)->Pmy      = NULL;
	(table)->TptBS    = NULL;
	(table)->TptNRTPS = NULL;
	(table)->TptRTPS  = NULL;
	(table)->TptUGS   = NULL;

}

CIDList *Get_CID_List()
{
	CIDList *p;
	p = (CIDList *)malloc(sizeof(CIDList));
	if(p == NULL)
	{
		printf("run out of memory");
		exit(1);
	}
	return(p);
}
void AddCIDList(CIDList **ptr,uint16 cid)
{
	CIDList *newnode;
	CIDList *head;
	newnode = Get_CID_List();
	newnode->cid = cid;
	newnode->next=NULL;
	head=*ptr;
	if(*ptr==NULL)
	{
		newnode->next=NULL;
		*ptr = newnode;
	}
	else
	{
		while((*ptr)->next!=NULL)
			*ptr=(*ptr)->next;
		(*ptr)->next=newnode;
		*ptr = head;
	}	
}




SDQ * Get_SDQ()
{
	SDQ *ptr;
	ptr = (SDQ * )malloc(sizeof(SDQ));
	if(ptr == NULL)
	{
		printf("run out of memory");
		exit(1);
	}
	return(ptr);
}
void Insert_SDQ(SDQ **dataptr,uint32 *addrptr,uint32 length,enum STATUS status)  
{   
	SDQ *newnode;                 
	SDQ *ptr;
	ptr = *dataptr;
	//Initial the newnode parameters
	newnode = Get_SDQ();
	newnode->ptr = addrptr;
	newnode->length = length;
	newnode->status = status;
	newnode->next = NULL;
	newnode->prev = NULL;
	if ( ptr == NULL )
	{
		ptr = newnode;
		ptr->prev = ptr;
		ptr->next = ptr;
	}
	else
	{
		if ( ptr->next == NULL )
		{              
			ptr->next = newnode ; 
			newnode->prev = ptr ; 
			newnode->next = ptr ; 
			ptr->prev = newnode;   
		}                        
		else                      
		{                        
			ptr->prev->next = newnode ; 
			newnode->prev = ptr->prev ; 
			newnode->next = ptr ;       
			ptr->prev = newnode ;       
		}                               
	}      
	*dataptr = ptr;
}                      
void Delete_SDQ(SDQ **ptr)
{
	if  ( *ptr == NULL )
		printf (" Deletion  of  head  node  not  permitted . \n" ) ;
	else if((*ptr)->next == (*ptr)->prev)
	{
		free(*ptr);
		*ptr = NULL;
	}
	else  {
		(*ptr) -> prev -> next  =  (*ptr) -> next ;
		(*ptr) -> next -> prev  =  (*ptr) -> prev;
		free ( *ptr ) ;
		(*ptr)=NULL;
	}
}

MDQ * Get_MDQ()
{
	MDQ *ptr;
	ptr = (MDQ * )malloc(sizeof(MDQ));
	if(ptr == NULL)
	{
		printf("run out of memory");
		exit(1);
	}
	return(ptr);
}
void Append_MDQ(MDQ **ptr,SDQ *sduQ,uint32 Qlength,uint32 cid,uint32 isARQ ,uint32 isfix)
{
	MDQ *newnode;
	MDQ *head;
	newnode = Get_MDQ();
	newnode->dataQ = sduQ;
	newnode->CID   = cid;
	newnode->Qlength = Qlength;
	newnode->fixVar = isfix;
	newnode->isARQ  = isARQ;
	newnode->next = NULL;
	head = *ptr;
	if(*ptr==NULL)
	{
		newnode->next=NULL;
		*ptr = newnode;
	}
	else
	{
		while((*ptr)->next!=NULL)
			*ptr=(*ptr)->next;
		(*ptr)->next=newnode;
		*ptr =head;
	}
}




void MoveMDQ(MDQ **desptr,MDQ **srcptr)
{
	MDQ *newnode=NULL;
	MDQ *headSrc=NULL;
	newnode = *srcptr;
	headSrc = (*desptr);
	(*srcptr) = (*srcptr)->next;
	newnode->next=NULL;
	if((*desptr)==NULL)
		(*desptr)=newnode;
	else
	{
		while((*desptr)->next!=NULL)
			(*desptr)=(*desptr)->next;
		(*desptr)->next=newnode;
		(*desptr)=headSrc;
	}
}

void GenSchRandSDU(uint32 *addrptr,SDQ **sduQ,uint32 isFix_Var , uint32 *packets)
{		
	uint32 ipackets=0;
	uint32 fixed_size = 10;
	//uint8 *tmptr=NULL;
//	uint32 ibytes=0;
	uint32 length=0;
	for(ipackets = 0 ; ipackets < *packets ; ipackets++)
	{
		if(isFix_Var)
		{
			length = fixed_size*10;
		}
		else
		{
			length = (rand()%10)*10;
		}
		//write bytes to SDU
		addrptr =(uint32 *)malloc(sizeof(uint8)*length);
		//tmptr=(uint8 *)addrptr;

		fread(addrptr,sizeof(uint8),length,InFileUsr1);
    if( feof(InFileUsr1) )      
    {
        //file_end = 1;
        //if(CFG_SHOW_MESSAGE)
            printf("File ended. \n");
			*packets=ipackets;
			return;
        
        //fclose(InFileUsr1);
        //fclose(OutFileUsr1);
        //fclose(decFile);

        //printf("Simulation Ended\n");
       ///printf("Total transmitted bytes = %15d\n", trans_byte);
       //getchar();
        //exit(0);
    }
/*
		for(ibytes=0;ibytes<length;ibytes++)
		{
			*tmptr=0x12;
			tmptr++;
		}
*/
		//			if(isFix_Var)
		Insert_SDQ(sduQ,addrptr,length,COMPLETED);
		//			else
		//				Insert_SDQ(sduQ,addrptr,length,VARLEN);
		addrptr=NULL;
	}
}
void GenSchSDU()
{
	SDQ  *sduQ=NULL;
	MDQ  *sduL=NULL;
	//	uint32 length=0;
	//	uint32 j=0;
	uint32  *addrptr=NULL;
	//	uint32 Qlength=0;
	uint32 usr=0;
	uint32 type=0;
	uint32 cid =0;
	uint32 isfix=0;
	uint32 isARQ=0;
	uint32 packets=0;

	//--- Generate SDU --- //
	for(usr=0;usr<tableMU2MI.usrCount;usr++)
	{
		for(type=0;type<1;type++)
		{
			packets = 2500 ;//rand() & 0x7;
			switch(type)
			{
			case 0:
				cid =tableMU2MI.usrAddr[usr]->cidGroup.TptUGSCID->cid;
				isfix = 1;
				GenSchRandSDU(addrptr,&sduQ,isfix ,&packets);

				break;
			case 1:
				cid = tableMU2MI.usrAddr[usr]->cidGroup.TptRTPSCID->cid;
				isfix = 1;//default 0
				GenSchRandSDU(addrptr,&sduQ,isfix ,&packets);
				break;
			case 2:
				cid = tableMU2MI.usrAddr[usr]->cidGroup.TptNRTPSCID->cid;
				isfix = 1;//default 0
				GenSchRandSDU(addrptr,&sduQ,isfix ,&packets);
				break;
			case 3:
				cid = tableMU2MI.usrAddr[usr]->cidGroup.TptBSCID->cid;
				isfix = 1;//default 0
				GenSchRandSDU(addrptr,&sduQ,isfix ,&packets);
				break;
			case 4: cid = tableMU2MI.usrAddr[usr]->cidGroup.PmyCID;
				isfix = 1;//default 0
				GenSchRandSDU(addrptr,&sduQ,isfix ,&packets);
				break;
			}
			Append_MDQ(&sduL,sduQ,packets,cid,isARQ,isfix);
			sduQ=NULL;
		}
	}
	sys.sysQ.bfSchSDUL = sduL;
}


uint32 SrhBTUsrByLp(CIT *cur ,uint32 cid)
{
	CIT *p =cur;
	while(p)
	{
		if(cid < p->CID)
		{
			p = p->left;
		}
		else if(cid > p->CID)
		{
			p = p->right;
		}
		else
		{
			return p->Usr;
		}
	}
	return 0;
}
void TwoChildren(CIT *p,CIT *pp)
{
	CIT *s=NULL;
	CIT *ps=NULL;
	if(p->left&&p->right)//two children
	{
		//conver to zero or one child case
		//find largest element in left subtree of p
		s=p->left;
		ps=p;//parent of s
		while(s->right)//move to larger element
		{
			ps=s;
			s=s->right;
		}
		p->Usr=s->Usr;
		p=s;
		pp=ps;
	}
}
void OneChild(CIT *p,CIT*pp,CIT *root)
{
	CIT *c = NULL;
	if(p->left)
		c = p->left;
	else
		c = p->right;
	if(p == root)	//delete p
		root=c;
	else
	{
		if(p == pp->left)		//is p left or right child of pp?
			pp->left=c;
		else
			pp->right=c;
	}
}
void ThrowBadInput()
{
	printf("Bad Input \n");
}
void DeleteCIDBT(CIT *root,uint32 cid)
{
	CIT *p = root;//search pointer
	CIT *pp = NULL;//parent of p
	while(p && p->CID != cid)//move to a child of p
	{
		pp = p;
		if(cid < p->CID)
			p = p->left;
		else
			p = p->right;
	}
	if(!p)
	{//no element with key
		ThrowBadInput();
		return ;
	}	
	//Restructure tree
	TwoChildren(p,pp);	//handle case when p hes two children
	OneChild(p,pp,root);	//p has at most one child//save child pointer in c
	free(p);
}




CIT *MakeTree(uint32 cid,uint32 usr)
{
	CIT *np;
	np=(CIT*)malloc(sizeof(CIT));
	if(np==NULL)
		return NULL;
	np->CID=cid;
	np->Usr=usr;
	np->left=np->right=NULL;
	return np;
}

void SrhCMT(CMT *table,uint32 cid,uint32 *usr,uint32 *type)
{
	if(cid==0x0)//initial ranging cid
	{
		*type = IRNG_CID;
		*usr  = 0x0;//no usr
	}
	else if(cid == 0xFFFF)//broadcast cid
	{
		*type = BROADCAST_CID;
		*usr = 0xFF;//all usr
	}
	else if(cid == 0xFFFE)//padding cid
	{
		*type = PADDING_CID;
		*usr  = 0xFF;//all usr
	}
	else if(cid<0x100)//basic cid
	{
		*type =BASIC_CID;
		*usr = SrhBTUsrByLp(table->Bsc , cid);
	}
	else if(cid<0x200)//primary cid
	{
		*type = PRIMARY_CID;
		*usr = SrhBTUsrByLp(table->Pmy , cid);
	}
	else if(cid<0x300)//transport UGS cid
	{
		*type = TRANSPORT_UGS_CID;
		*usr = SrhBTUsrByLp(table->TptUGS , cid);
	}
	else if(cid<0x400)//transport UGS cid
	{
		*type = TRANSPORT_RTPS_CID;
		*usr = SrhBTUsrByLp(table->TptRTPS , cid);
	}
	else if(cid<0x500)//transport UGS cid
	{
		*type = TRANSPORT_NRTPS_CID;
		*usr = SrhBTUsrByLp(table->TptNRTPS , cid);
	}
	else if(cid<0x600)//transport UGS cid
	{
		*type = TRANSPORT_BS_CID;
		*usr = SrhBTUsrByLp(table->TptBS , cid);
	}
}



int InsCID2CMT(CIT **root,uint32 cid ,uint32 usr)
{
	CIT *p=*root;
	CIT *pp=0;
	while(p)
	{
		pp=p;
		if(cid<p->CID)
			p=p->left;
		else if(cid > p->CID)
			p=p->right;
		else
		{
			ThrowBadInput();
			return 0;
		}
	}
	if(*root)
	{
		if(cid < pp->CID)
			pp->left = MakeTree(cid,usr);
		else
			pp->right = MakeTree(cid,usr);
	}
	else
		*root =MakeTree(cid,usr);
	return 1;
}


uint16 AddCMTCID(CIT **root,uint32 usr,uint16 range,uint16 base)
{
	uint16 cid = (uint16)(rand()%range)|base;
	while(!InsCID2CMT(root,cid,usr))
	{
		cid=(uint16)(rand()%range)|base;
	}
	return cid;
}


void DelCMT(CMT table,enum CID_Type type,uint32 cid)
{
	switch(type)
	{
	case BASIC_CID:
		DeleteCIDBT( table.Bsc, cid);
		break;
	case PRIMARY_CID:
		DeleteCIDBT(table.Pmy, cid);
		break;
	case TRANSPORT_UGS_CID:
		DeleteCIDBT(table.TptUGS, cid);
		break;
	case TRANSPORT_RTPS_CID:
		DeleteCIDBT(table.TptRTPS,cid);
		break;
	case TRANSPORT_NRTPS_CID:
		DeleteCIDBT(table.TptNRTPS, cid);
		break;
	case TRANSPORT_BS_CID:
		DeleteCIDBT(table.TptBS, cid);
		break;
	default :
		break;
	}

}
void AddCMT(CMT *table,enum CID_Type type,uint32 usr)
{
	uint16 cid;
	CG *cidgroup = &tableMU2MI.usrAddr[usr]->cidGroup;
	switch(type)
	{
	case BASIC_CID:
		cid = AddCMTCID(&table->Bsc,usr,BscRange,BscBase);
		cidgroup->BscCID=cid;
		break;
	case PRIMARY_CID:
		cid =AddCMTCID(&table->Pmy,usr,PmyRange,PmyBase);
		cidgroup->PmyCID=cid;
		break;
	case TRANSPORT_UGS_CID:
		cid = AddCMTCID(&table->TptUGS,usr,TptUgsRange,TptUgsBase);
		AddCIDList(&cidgroup->TptUGSCID,cid);
		break;
	case TRANSPORT_RTPS_CID:
		cid = AddCMTCID(&table->TptRTPS,usr,TptRtpsRange,TptRtpsBase);
		AddCIDList(&cidgroup->TptRTPSCID,cid);
		break;
	case TRANSPORT_NRTPS_CID:
		cid = AddCMTCID(&table->TptNRTPS,usr,TptNrtpsRange,TptNrtpsBase);
		AddCIDList(&cidgroup->TptNRTPSCID,cid);
		break;
	case TRANSPORT_BS_CID:
		cid = AddCMTCID(&table->TptBS,usr,TptBsRange,TptBsBase);
		AddCIDList(&cidgroup->TptBSCID,cid);
		break;
	default :
		break;
	}
}




void TwoChildrenCMIT(CMIT *p,CMIT *pp)
{
	CMIT *s=NULL;
	CMIT *ps=NULL;
	if(p->left&&p->right)//two children
	{
		//conver to zero or one child case
		//find largest element in left subtree of p
		s=p->left;
		ps=p;//parent of s
		while(s->right)//move to larger element
		{
			ps=s;
			s=s->right;
		}
		p->mdqptr=s->mdqptr;
		p=s;
		pp=ps;
	}
}
void OneChildCMIT(CMIT *p,CMIT **pp,CMIT **root)
{
	CMIT *c = NULL;
	if(p->left)
		c = p->left;
	else
		c = p->right;
	if(p == *root)	//delete p
		*root=c;
	else
	{
		if(p == (*pp)->left)		//is p left or right child of pp?
			(*pp)->left=c;
		else
			(*pp)->right=c;
	}
}
void DeleteCMIT(CMIT **root,uint32 cid)
{
	CMIT *p = *root;//search pointer
	CMIT *pp = NULL;//parent of p
	while(p && (p->CID != cid))//move to a child of p
	{
		pp = p;
		if(cid < p->CID)
			p = p->left;
		else
			p = p->right;
	}
	if(!p)
	{//no element with key
		ThrowBadInput();
		return ;
	}	
	//Restructure tree
	TwoChildrenCMIT(p,pp);	//handle case when p hes two children
	OneChildCMIT(p,&pp,root);	//p has at most one child//save child pointer in c
	free(p);
}

MDQ * SrhBTMdqPtrByLp(CMIT *cur ,uint32 cid)
{
	CMIT *p =cur;
	while(p)
	{
		if(cid < p->CID)
		{
			p = p->left;
		}
		else if(cid > p->CID)
		{
			p = p->right;
		}
		else
		{
			return p->mdqptr;
		}
	}
	return 0;
}
CMIT *MakeCMIT(uint32 cid,MDQ *mdqptr)
{
	CMIT *np;
	np=(CMIT*)malloc(sizeof(CMIT));
	if(np==NULL)
		return NULL;
	np->CID=cid;
	np->mdqptr = mdqptr;
	np->left=np->right=NULL;
	return np;
}
int InsCID2CMIT(CMIT **root,uint32 cid ,MDQ **mdqptr)
{
	CMIT *p=*root;
	CMIT *pp=0;
	while(p)
	{
		pp=p;
		if(cid<p->CID)
			p=p->left;
		else if(cid > p->CID)
			p=p->right;
		else
		{
			ThrowBadInput();
			return 0;
		}
	}
	if(*root)
	{
		if(cid < pp->CID)
			pp->left = MakeCMIT(cid,*mdqptr);
		else
			pp->right = MakeCMIT(cid,*mdqptr);
	}
	else
		*root =MakeCMIT(cid,*mdqptr);
	return 1;
}


void Format2Burst()
{
	uint32 usr  = 0;
	//process usr DLQS ==> ugs rtps nrtps bs msgq
	while(sys.sysQ.pduQ != NULL)
	{
		Form_Burst(&sys.sysQ.burstQ,&sys.sysQ.pduQ);
	}

	while(usr < tableMU2MI.usrCount)
	{
		//Insert_SDQ(SDQ **dataptr,uint32 *addrptr,uint32 length,enum STATUS status)  
		if(tableMU2MI.usrAddr[usr]->dlQueues.pduQ != NULL)
			Form_Burst(&tableMU2MI.usrAddr[usr]->dlQueues.burstQ,&tableMU2MI.usrAddr[usr]->dlQueues.pduQ);
		usr++;
	}

}
SDQ * TransmitBroBurst()
{
	SDQ *dataQptr;
	MDQ *tmpmdq = NULL;
	int agglength = 0;
	uint8 *baseptr = NULL;
	tmpmdq = sys.sysQ.broadcastQ;

	while(tmpmdq != NULL)
	{
		agglength += tmpmdq->dataQ->length;
		tmpmdq = tmpmdq->next;
	}
	agglength += sys.sysQ.burstQ->length;

	dataQptr = (SDQ *)malloc(sizeof(SDQ));
	dataQptr->length = agglength;
	dataQptr->status = 0;
	dataQptr->next = dataQptr->prev = NULL;
	dataQptr->ptr = (uint32 *)malloc(sizeof(uint32)*agglength);
	baseptr = (uint8 *)dataQptr->ptr;

	tmpmdq = sys.sysQ.broadcastQ;	
	while(tmpmdq != NULL)
	{
		memcpy(baseptr,tmpmdq->dataQ->ptr,tmpmdq->dataQ->length);
		baseptr += tmpmdq->dataQ->length;
		Delete_PDUQ(&tmpmdq);
	}
	sys.sysQ.broadcastQ = tmpmdq ;	
	memcpy(baseptr,sys.sysQ.burstQ->ptr,sys.sysQ.burstQ->length);
	Delete_SDQ(&sys.sysQ.burstQ);
	return dataQptr;

}

SDQ * TransmitUsrBurst(SDQ **burst)
{
	SDQ *newptr;
	newptr = (SDQ *)malloc(sizeof(SDQ));
	newptr->length = (*burst)->length;
	newptr->status = 0;
	newptr->next = newptr->prev = NULL;
	newptr->ptr = (uint32 *)malloc(sizeof(uint32)*newptr->length);
	memcpy(newptr->ptr,(*burst)->ptr,newptr->length);
	Delete_SDQ(burst);
	*burst = NULL;
	return newptr;
}
void AppendCID()
{
	uint32 usr = 0;
	for(usr=0;usr<USER;usr++)
	{
		usr = CreateSI();
		AddCMT(&tableCMT,BASIC_CID,usr);
		AddCMT(&tableCMT,PRIMARY_CID,usr);
		AddCMT(&tableCMT,TRANSPORT_UGS_CID,usr);
		AddCMT(&tableCMT,TRANSPORT_RTPS_CID,usr);
		AddCMT(&tableCMT,TRANSPORT_NRTPS_CID,usr);
		AddCMT(&tableCMT,TRANSPORT_BS_CID,usr);
		InitMU2MI(usr);
	}
	Append_SC_CID(&scid,0xFFFF,BROADCAST_CID);
	Append_SC_CID(&scid,tableMU2MI.usrAddr[0]->cidGroup.BscCID,BASIC_CID);
	Append_SC_CID(&scid,tableMU2MI.usrAddr[0]->cidGroup.PmyCID,PRIMARY_CID);
	Append_SC_CID(&scid,tableMU2MI.usrAddr[0]->cidGroup.TptUGSCID->cid,TRANSPORT_UGS_CID);
	Append_SC_CID(&scid,tableMU2MI.usrAddr[0]->cidGroup.TptRTPSCID->cid,TRANSPORT_RTPS_CID);
	Append_SC_CID(&scid,tableMU2MI.usrAddr[0]->cidGroup.TptNRTPSCID->cid,TRANSPORT_NRTPS_CID);
	Append_SC_CID(&scid,tableMU2MI.usrAddr[0]->cidGroup.TptBSCID->cid,TRANSPORT_BS_CID);
	Append_SC_CID(&scid,0XFFFE,PADDING_CID);
}
void InitProc()
{

	//char filename1[20];
	//char filename2[20];
	char testwords[194661];
	InitSysInfo();
	InitMsgTLVTable();
	InitCMT(&tableCMT);
	//printf("type Input File Name\n");
	//printf("type Output File Name\n");
	//scanf("%s %s",&filename1,&filename2);
	InFileUsr1  = fopen(TestInFileName , "rb");
	OutFileUsr1 = fopen(TestOutFileName, "wb");
	//fread(testwords,sizeof(char),194661,InFileUsr1);
	//fwrite(testwords,sizeof(char),194661,OutFileUsr1);
	//fclose(InFileUsr1);
	//fclose(OutFileUsr1);

}
#ifdef _NonSDL_

int main()
{
	uint32 iusr=0;
	uint32 iframe=0;
	BWRList *bwrlist = NULL;
	//	SDQ *newburst = NULL;
	BWRList *headbwrlist = NULL;
	SDQ *usrburst =NULL;
	SDQ *broadcastburst = NULL;
	MsgList *msglist = NULL;
	scid = NULL;
	InitProc();

	
	//InitCMIT(&sys.rmdPDUCMIT);
	//CreateSI();
	AppendCID();
	//+++ initial in rng process +++
	
	//+++ initial in dispatcher +++
	GenSchSDU();
	//+++ initial in control plane process +++
	PrintMsg("----------","UL SubFrame","----------");
	InitControlMMsg();
	printf("\n\n");
	
	for(iframe=0;iframe<120;iframe++)
	{
	
	ULSchedulerMain(headbwrlist);
	DLSchedulerMain();
	PrintSystemTime("-------- Time ",iframe*DURATION ,"ms ----------");
	PrintMsg("----------","DL SubFrame","----------");
	PrintMsg("DL channel","[ Broadcast ]","burst to MS");
	for(iusr = 0 ;iusr < tableMU2MI.usrCount;iusr++)
	{
		if(tableMU2MI.usrAddr[iusr]->dlQueues.burstQ != NULL)
		{
			usrburst = TransmitUsrBurst(&tableMU2MI.usrAddr[iusr]->dlQueues.burstQ);
			PrintMsg("DL channel","[ Unicast ]","burst to MS");
		}
		else
			usrburst = NULL;
	}
	
	broadcastburst = TransmitBroBurst();

	DeformatBroadcastBurst(broadcastburst,&msglist);

	DeformatUsrBurst(usrburst,&msglist,&bwrlist);
	PrintMsg("----------","UL SubFrame","----------");
	}
        fclose(InFileUsr1);
        fclose(OutFileUsr1);
return 0;	
}
#endif